﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using BLL.Interface.Base;

namespace BLL.Implement.Base
{
    public class ValidateCodeImpl : IValidateCode
    {
        private static readonly char[] Chars = new[]
                                           {
                                               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
                                               'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
                                               'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
                                               'u', 'v', 'w', 'x', 'y', 'z'
                                           };

        public string CreateCode(int length)
        {
            Random r = new Random();
            StringBuilder code = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                var currentChar = Chars[r.Next(36)];
                code.Append(currentChar);
            }
            return code.ToString();
        }

        public byte[] CreateImage(string validateCode, int imageHeight)
        {
            Bitmap image = new Bitmap(GetImageWidth(validateCode.Length), imageHeight);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            try
            {
                Random random = new Random();
                //画图片的背景噪音线
                for (int i = 0; i < 12; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 20, FontStyle.Bold | FontStyle.Italic);
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateCode, font, brush, 3, 2);

                //画图片的前景干扰点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                return stream.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        /// <summary>
        /// 得到验证码图片的长度
        /// </summary>
        /// <param name="validateNumLength">验证码的长度</param>
        /// <returns></returns>
        private static int GetImageWidth(int validateNumLength)
        {
            return (int)(validateNumLength * 22.0);
        }


    }
}